package com.baichang.taoshunda.dao.impl;

import org.springframework.stereotype.Repository;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.taoshunda.dao.FavoritesDao;
import com.baichang.taoshunda.entity.Favorites;

import java.util.List;
import java.util.Map;

@Repository
public class FavoritesDaoImpl extends BaseDaoImpl<Favorites> implements FavoritesDao {
    /**
     * 检查是否收藏
     *
     * @param bussId 商家编号
     * @param userId 用户编号
     * @return
     */
    @Override
    public int getIsFavoritesByUserId(int bussId, int userId) {
        String sql = "SELECT COUNT(0) AS count  FROM t_favorites WHERE user_id=? AND favorite_id=?";
        return queryForInt(sql, userId, bussId);
    }

    /**
     * 我的收藏列表
     *
     * @param userId   用户编号
     * @param typeId   收藏类型   1普通商家 2本地服务商家 3商品
     * @param nowPage
     * @param pageSize @return
     */
    @Override
    public List<Map<String, Object>> myFavoites(int userId, int typeId, int nowPage, int pageSize) {
        String sql;
        if (typeId == 1) {
            sql = "SELECT f.id AS favId, f.user_id,b.buss_name AS bussName,b.is_invoice AS isInvoice, b.lng, b.lat, b.id, b.start_time AS startTime , b.end_time AS endTime, b.is_open AS isOpen,b.store_pic as bussLogo, b.state, b.dispatching ,(SELECT IFNULL(SUM(( SELECT SUM(od.count) FROM t_order_details od WHERE od.order_number = o.order_number )), 0) AS count FROM t_order o WHERE o.seller_id =  b.id AND order_state IN (5, 6, 9, 13, 14, 15, 16) AND DATE_FORMAT(created, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m')) AS monthCount, ( SELECT COUNT(id) AS count FROM t_goods g WHERE g.company_id = b.id AND `status` = 1 and is_upper=1) AS goodsCount, ( SELECT IFNULL(AVG(star_num), 0) AS number FROM t_goods_comment gc WHERE gc.bus_id = b.id ) AS starNumber, f.created, ( SELECT COUNT(0) FROM `t_business_money_record` WHERE `buiness_ud` = b.id AND `status` = '1' AND type = 1 ) AS isRecord FROM t_favorites f INNER JOIN t_bussiness b ON b.id = f.favorite_id WHERE f.type_id = ? AND f.user_id = ? AND b.state=1";
        } else if (typeId == 2) {
            sql = "SELECT f.id AS favId, f.user_id, f.favorite_id, f.created, w.*,w.provinces_id AS provincesId,w.if_payment AS ifPayment,w.region_id AS regionId,w.city_id AS cityId,w.list_pic AS listPic,w.waiter_type AS waiterType,w.phone_name AS phoneName,w.waiter_name AS waiterName,\n" +
                    "w.waiter_adress AS waiterAdress,w.personal_pic AS personalPic,w.business_pic AS businessPic FROM t_favorites f INNER JOIN t_waiter_list w ON f.favorite_id = w.id WHERE f.type_id = ? AND f.user_id=?  and w.status=1 ";
        } else {
            sql = "SELECT f.id, g.id AS goodsId, g.NAME AS goodsName, g.is_bespeak isBespeak, g.is_free isFree, g.unit, g.is_home_installation isHomeInstallation, g.is_no_reason_return isNoReasonReturn, g.commercial_activities commercialActivities, g.box_price , g.head_pic headPic, g.price, g.month_sales saleNum, g.company_id AS companyId , IFNULL(( SELECT SUM(number) FROM t_shop_car sc WHERE sc.goods_id = g.id AND sc.user_id = " + userId + " ), 0) AS shopCarNum FROM t_favorites f INNER JOIN t_goods g ON g.id = f.favorite_id AND g.`status` = 1 AND g.is_upper = 1 WHERE f.type_id = ? and f.user_id = ?;";
        }
        return listForMapList(sql, nowPage, pageSize, typeId, userId);
    }

    @Override
    public int getCountByUserAndBusId(int bussId, int userId, int typeId) {
        String sql = "SELECT COUNT(0) AS count  FROM t_favorites WHERE user_id=? AND favorite_id=? and type_id=?";
        return queryForInt(sql, userId, bussId, typeId);
    }

    @Override
    public void deleteByBussId(int userId, int bussId, int typeId) {

        delete("user_id=? AND type_id=? AND favorite_id=?", userId, typeId, bussId);
    }

    @Override
    public int getTotal(int userId, int typeId) {
        String sql;
        if (typeId == 1) {
            sql = "SELECT count(0) FROM t_favorites f INNER JOIN t_bussiness b ON b.id = f.favorite_id WHERE f.type_id = ? AND user_id = ? AND b.state=1";
        } else {
            sql = "SELECT count(0) FROM t_favorites f INNER JOIN t_waiter_list w ON f.favorite_id = w.id WHERE f.type_id = ? AND f.user_id=?";
        }
        return queryForInt(sql, typeId, userId);
    }

    /**
     * @param id
     * @param userId
     * @return
     */
    @Override
    public Map<String, Object> queryWaiterBusienss(Integer id, int userId) {
        String sql = "SELECT count(*) AS num FROM t_favorites WHERE user_id=? AND favorite_id=? AND type_id=2";
        return queryForMap(sql, userId, id);
    }
}